
local _PATH = (...):gsub('%.pathfinding$','')

-- Interface to Jumper
local Grid = require(_PATH .. '.jumper.grid')
local Pathfinder = require(_PATH .. '.jumper.pathfinder')

-- The grid runs from 0 to mapSize+1 so that AI slightly off grid are still processed

function initPathfinding()

	jumperMap = {}
	for i=0,worldData.size.x+1,1 do
		jumperMap[i] = {}
		for j = 0,worldData.size.y+1 do
			jumperMap[i][j] = 0
		end
	end

	jumperGrid = Grid(jumperMap)
	jumperFinder = Pathfinder(jumperGrid, 'ASTAR', 0) 


	return
end

function GetWalls()
	out = "walls: "
	for i=1,worldData.size.x,1 do
		for j = 1,worldData.size.y do
			if (jumperMap[i][j] == 1) then out = out .."("..i..","..j..") " end
		end
	end

	return out
end


function updatePathfinding(gameMap)

	for i=0,worldData.size.x+1,1 do
		for j = 0,worldData.size.y+1 do

			if isASBlocked(j,i) == true then -- I dont know, dont as me. Jumper seems to switch .x and .y
				jumperMap[i][j] = 1
			else
				jumperMap[i][j] = 0
			end

			-- offMap always walkable
			if (i == 0 or i == worldData.size.x+1) then jumperMap[i][j] = 0 end
			if (j == 0 or j == worldData.size.y+1) then jumperMap[i][j] = 0 end

		end
	end

	jumperGrid = Grid(jumperMap)
	jumperFinder = Pathfinder(jumperGrid, 'ASTAR', 0) 

	return
end

function getPath(startLoc, endLoc)
	return jumperFinder:getPath(startLoc[1],startLoc[2],endLoc[1],endLoc[2],true)
end